<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生成器函数参数</title>
</head>

<body>
    <script>

        /*
         * next 方法可以传递实参, 这个实参会替代上一次yield执行的结果
         *
         */

        function* gen(arg) {
            console.log(arg);
            let one = yield 111;
            console.log(one);
            let two = yield 222;
            console.log(two);
            let three = yield 333;
            console.log(three);
        }

        // 执行获取迭代器对象
        let iterator = gen('AAA');
        console.log(iterator.next()); //  'AAA'  {value:111, done: false}
        console.log(iterator.next("BBB")); // 'BBB'  {value:222, done: false}   next方法可以传递参数
        console.log(iterator.next('CCC')); // 'CCC'  {value:333, done: false}
        console.log(iterator.next('DDD')); // 'DDD'  {value:undefined, done: true}



        function* gen2() {
            let one = yield 666;
            console.log(one);
        }

        let iterator2 = gen2();
        console.log(iterator2.next()); // {value:666, done:false}
        /*
         * 在第一次执行yield 666的时候, 就将结果返回,但是没有执行one的赋值;
         * 在第二次执行时, one的值就是undefined
         *
         */
        console.log(iterator2.next()); // undefined  {value:undefined, done: true}
     


        // 解决方法一
        function* gen3() {
            let one = yield 666;
            console.log(one);
        }

        let iterator3 = gen3();
        i = iterator3.next(); 
        iterator3.next(i.value);

        // 解决方法二
        function* gen4() {
            let one;
            yield one = 666;
            console.log(one);
        }

        let iterator4 = gen4();
        i = iterator4.next(); 
        iterator4.next();

    </script>
</body>

</html>